package com.mlink.ai.chat.utils

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.view.View
import androidx.appcompat.content.res.AppCompatResources
import com.mlink.ai.chat.assistant.robot.R

object AnimationUtil {

    fun startShakeAndFlashAnimation(context: Context, btn: View, light: View?): AnimatorSet {
        val shakeAnimator: ObjectAnimator =
            ObjectAnimator.ofFloat(btn, "translationX", 0f, 25f, -25f, 15f, -15f, 6f, -6f, 0f)
        shakeAnimator.duration = 1000
        shakeAnimator.startDelay = 200
        shakeAnimator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                if (light != null) {
                    light.visibility = View.VISIBLE
                }
            }
        })

        val flashImg = AppCompatResources.getDrawable(context, R.drawable.vip_btn_lightning)
        val flashWidth = flashImg?.intrinsicWidth ?: 0
        val width: Int = btn.width
        val flashAnimator: ObjectAnimator =
            ObjectAnimator.ofFloat(light, "translationX", 0f, (width + flashWidth).toFloat())
        flashAnimator.duration = 1500
        val animatorSet = AnimatorSet()
        animatorSet.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                super.onAnimationEnd(animation)
                animatorSet.start()
            }
        })
        animatorSet.playSequentially(shakeAnimator, flashAnimator)
        return animatorSet
    }
}